$(document).ready(function(){
	//setBackground();
	var locationHref = document.location.href.split("=");
	if(locationHref.length > 1){
		$(".cases-div").hide();
		$(".case-show").show();
		$(".case-detail").hide();
	hrefId = locationHref[1];
		$.ajax({
			url:"http://www.leveldisplay.com/getCaseById.php",
			method:"post",
			data:"id="+hrefId,
			success:function(e){
				var caseJsonList = JSON.parse(e);
				listLength = caseJsonList.length;
				var showCount = 0;
				var imgCount =0;
				changeCaseInfo();
				//timerCase = setInterval(changeCaseInfo,8000);
				function changeCaseInfo(){
					$(".show-info .english-text").html(caseJsonList[showCount].name_en);
					$(".show-info .chinese-text").html(caseJsonList[showCount].name_cn);
					$(".show-info .content").html(caseJsonList[showCount].detail);
					$(".detail-1 p").html(caseJsonList[showCount].show);
					$(".detail-2 p").html(caseJsonList[showCount].area);
					$(".detail-3 p").html(caseJsonList[showCount].time);
					$(".detail-4 p").html(caseJsonList[showCount].management);
					$(".detail-5 p").html(caseJsonList[showCount].place);
					$(".detail-6 p").html(caseJsonList[showCount].fabrication);
					$(".show-deatil-pic img").attr("src","/case_image/"+caseJsonList[showCount].case_image[0].filename);
					showCount = showCount + 1;
					if(showCount >= listLength){
						showCount = 0;
					}
					var imgList = caseJsonList[showCount].case_image;
					var imgListLength = imgList.length
					if(imgListLength == 1){
						$(".detail-right-btn").hide();
						$(".detail-left-btn").hide();
					}else{
						$(".detail-right-btn").show();
						$(".detail-left-btn").show();
					}
					$(".detail-right-btn").click(function(){
						//if(imgCount < imgListLength){
							picCount1 = picCount1 + 1;

							$(".detail-left-btn").show();
							$(".show-deatil-pic img").attr("src","/case_image/"+caseJsonList[showCount].case_image[picCount1].filename);
							for(var i = 0; i < imgListLength; i++){
								$("#surprised li:eq("+i+") img").css("opacity",0);
								$("#surprised li:eq("+i+") img").hide();
							}
							$("#surprised li:eq("+(picCount1)+") img").css("opacity",1);
							$("#surprised li:eq("+picCount1+") img").show();
							if(picCount1+1 == imgListLength){
									console.log(1);
								$(".detail-right-btn").hide();
							}
							
							
						//}
					});
					$(".detail-left-btn").click(function(){
							console.log(picCount1);
							console.log(imgListLength);
							$(".detail-right-btn").show();
							picCount1 = picCount1 - 1;
							$(".show-deatil-pic img").attr("src","case_image/"+caseJsonList[showCount].case_image[picCount1].filename);
							for(var i = 0; i < imgListLength; i++){
								$("#surprised li:eq("+i+") img").css("opacity",0);
								$("#surprised li:eq("+i+") img").hide();
							}
							$("#surprised li:eq("+(picCount1)+") img").css("opacity",1);
							$("#surprised li:eq("+picCount1+") img").show();
							if(picCount1 == 0){
								$(".detail-left-btn").hide();
							}
						//}
					});
					$(".detail-pic").click(function(){
						console.log("123123");
						clearInterval(backTimer);
						$(".show-detail").hide();
						$(".show-deatil-pic").show();
						$(".show-deatil-pic img").attr("src","case_image/"+caseJsonList[showCount].case_image[picCount1].filename);
					});
					var imgListHtml = "";
					console.log(caseJsonList[showCount].case_image);
					for(var i = 0; i < caseJsonList[showCount].case_image.length;i++){
						imgListHtml += "<li><img src = '/case_image/"+caseJsonList[showCount].case_image[i].filename+"'></li>";
					}
					$("#surprised").html(imgListHtml);
					$("#surprised li:eq("+(imgCount)+") img").css("opacity",1);
					//changeBackground();

				}
			}
		});
	}
	else{
		 $.ajax({
            url:"http://www.leveldisplay.com/caseSearchList.php",
            method:"post",
            data:"year=0&type=0",
            success:function(e){
                    var caseList = JSON.parse(e);
                    var caseListHtml = "";
                    for(var i = 0;i<caseList.length;i++){
                            caseListHtml+= '<li data-caseId = '+caseList[i].id+'><img src="bg_image/'+caseList[i].bg_image+'"><p class = "case-summary">'+caseList[i].name_en+'</p><p class = "case-summary-chinese">'+caseList[i].name_ch+'</p></li>'
                    }
                    $("#case-exhibition").html(caseListHtml);
            }
    	});
	
	 }
	var backTimer,picCount1,picCount2;
	$(window).resize(function(){
		setBackground();
		if($(window).width() < 1027){
			$("#background-img").width(1027);
			$("#surprised .slide-01 img").width(1027);
		}else{
			$("#background-img").width($(window).width());
		$("#surprised .slide-01 img").width($(window).width());	
		}
		
		//$(".float-div").css(".")
		
	});
	//var newChanger = new changeUtil();
	//var newChanger = new $Class(changeUtil);
	changeBackground();
	setBackground();
	$(".weixin-img").mouseenter(function(e){
		$(".code-for-weixin").show();
	});
	$(".weixin-img").mouseleave(function(e){
		$(".code-for-weixin").hide();
	});
	//鼠标移入移出导航坚挺
	$(".nav").delegate("a","mouseenter",function(e){
		$(e.target).prev().css("display","inline-block");
		$(e.target).next().css("display","inline-block");
	});
	$(".nav").delegate("a","mouseleave",function(e){
		$(e.target).prev().css("display","none");
		$(e.target).next().css("display","none");
	});
	var canMoveMouseFlag = true;
	$(".for-left-nav").mouseenter(function(e){
		if(canMoveMouseFlag){
			canMoveMouseFlag = false;
			$(".nav-top").animate({left: '266px'}, "slow");
			$(".nav-bottom").animate({left: '266px'}, "slow",function(){
				canMoveMouseFlag = true;
			});
		}
		$(".menu a").css("background","url(res/menu2.png) no-repeat");
		$(".menu a").css("background-position","right center");
		$(".level-pager").css("display","none");
	});
	$(".left-nav").mouseleave(function(e){
		if(canMoveMouseFlag){
			canMoveMouseFlag = false;
			$(".nav-top").animate({left: '158px'}, "slow");
			$(".nav-bottom").animate({left: '-7px'}, "slow",function(){
				canMoveMouseFlag = true;
				$(".level-pager").css("display","block");
			});	
		}
		$(".menu a").css("background","url(res/menu.png) no-repeat");
		$(".menu a").css("background-position","right center");
	});
	//hover背景效果

	setHeight();
	var canMove = false;
	var flag = 1;
	//pager
	$(".left-nav").mouseenter(function(e){
		$(".level-pager").css("display","none");
	});
	$(".left-nav").mouseleave(function(e){
		$(".level-pager").css("display","block");
	});
	var beforeA = 0,
		clickCount = 0;
		var liclickCount = 0;
		var ulCount = 0;
		var libeforeA = 0;
	$(".case-title").delegate("a","click",function(e){
		$(".case-detail ul:eq("+parseInt(ulCount)+") li:eq("+parseInt(libeforeA)+")").removeClass("selected");
                $(".case-year a:eq("+parseInt(beforeAYear)+")").removeClass("selected");
		if(!$(this).hasClass("selected")){
			if(clickCount == 0){
				$(this).addClass("selected");	
				beforeA = $(this).index();
				clickCount += clickCount +1;
				//$(".case-detail ul:eq("+$(this).index()+")").fadeIn("fast");
			}else{
				$(this).addClass("selected");	
				//$(".case-detail ul:eq("+$(this).index()+")").fadeIn("slow");
				$(".case-title a:eq("+parseInt(beforeA)+")").removeClass("selected");
				//$(".case-detail ul:eq("+parseInt(beforeA)+")").fadeOut("fast");
				beforeA = $(this).index();
			}
		}
		ulCount = $(this).index();
		liclickCount = 0;
	});
	var beforeAYear = 0,
		clickCountYear = 0;
	var liclickCountYear = 0;
	var ulCountYear = 0;
	var libeforeAYear = 0;
	$(".case-year").delegate("a","click",function(){
		$(".case-detail ul:eq("+parseInt(ulCountYear)+") li:eq("+parseInt(libeforeAYear)+")").removeClass("selected");
		if(!$(this).hasClass("selected")){
			if(clickCountYear == 0){
				$(this).addClass("selected");	
				beforeAYear = $(this).index();
				clickCountYear += clickCountYear +1;
				//$(".case-detail ul:eq("+$(this).index()+")").fadeIn("fast");
			}else{
				$(this).addClass("selected");	
				//$(".case-detail ul:eq("+$(this).index()+")").fadeIn("slow");
				$(".case-year a:eq("+parseInt(beforeAYear)+")").removeClass("selected");
				//$(".case-detail ul:eq("+parseInt(beforeAYear)+")").fadeOut("fast");
				beforeAYear = $(this).index();
			}
		}
		ulCountYear = $(this).index();
		liclickCountYear = 0;
	});
	var typeId=0,yearId=0;
	$(".case-title").delegate("a","click",function(ev){
		typeId = $(this).attr("data-id");
                yearId = 0;
                $.ajax({
                        url:"http://www.leveldisplay.com/caseSearchList.php",
                        method:"post",
                        data:"year="+yearId+"&type="+typeId,
                        success:function(e){
                                var caseList = JSON.parse(e);
                                var caseListHtml = "";
                                for(var i = 0;i<caseList.length;i++){
                                        caseListHtml+= '<li data-caseId = '+caseList[i].id+'><img src="bg_image/'+caseList[i].bg_image+'"><p class = "case-summary">'+caseList[i].name_en+'</p><p class = "case-summary-chinese">'+caseList[i].name_ch+'</p></li>'
                                }
                                $("#case-exhibition").html(caseListHtml);
                        }
                });
	});
	$(".case-year").delegate("a","click",function(ev){
		yearId = $(this).html();
                $.ajax({
                        url:"http://www.leveldisplay.com/caseSearchList.php",
                        method:"post",
                        data:"year="+yearId+"&type="+typeId,
                        success:function(e){
                                var caseList = JSON.parse(e);
                                var caseListHtml = "";
                                for(var i = 0;i<caseList.length;i++){
                                        caseListHtml+= '<li data-caseId = '+caseList[i].id+'><img src="bg_image/'+caseList[i].bg_image+'"><p class = "case-summary">'+caseList[i].name_en+'</p><p class = "case-summary-chinese">'+caseList[i].name_ch+'</p></li>'
                                }
                                $("#case-exhibition").html(caseListHtml);
                        }
                });
	});
	$(".case-detail ul").delegate("li","click",function(){
		if(!$(this).hasClass("selected")){
			if(liclickCount == 0){
				$(this).addClass("selected");	
				libeforeA = $(this).index();
				liclickCount += liclickCount +1;
			}else{
				$(this).addClass("selected");	
				$(".case-detail ul:eq("+parseInt(ulCount)+") li:eq("+parseInt(libeforeA)+")").removeClass("selected");
				libeforeA = $(this).index();
			}
		}
	});
	var timerCase;
	var hrefId;
	$("#case-exhibition").delegate("li","click",function(e){
		clearInterval(timerCase);
		clearInterval(backTimer);
		hrefId = $(this).attr("data-caseId");
		$(".case-show").show();
		$('.cases-div').hide();
		$(".case-detail").hide();
		$.ajax({
			url:"http://www.leveldisplay.com/getCaseById.php",
			method:"post",
			data:"id="+hrefId,
			success:function(e){
				var caseJsonList = JSON.parse(e);
				listLength = caseJsonList.length;
				var showCount = 0;
				var imgCount =0;
				changeCaseInfo();
				//timerCase = setInterval(changeCaseInfo,8000);
				function changeCaseInfo(){
					$(".show-info .english-text").html(caseJsonList[showCount].name_en);
					$(".show-info .chinese-text").html(caseJsonList[showCount].name_cn);
					$(".show-info .content").html(caseJsonList[showCount].detail);
					$(".detail-1 p").html(caseJsonList[showCount].show);
					$(".detail-2 p").html(caseJsonList[showCount].area);
					$(".detail-3 p").html(caseJsonList[showCount].time);
					$(".detail-4 p").html(caseJsonList[showCount].management);
					$(".detail-5 p").html(caseJsonList[showCount].place);
					$(".detail-6 p").html(caseJsonList[showCount].fabrication);
					$(".show-deatil-pic img").attr("src","/case_image/"+caseJsonList[showCount].case_image[0].filename);
					showCount = showCount + 1;
					if(showCount >= listLength){
						showCount = 0;
					}
					var imgList = caseJsonList[showCount].case_image;
					var imgListLength = imgList.length
					if(imgListLength == 1){
						$(".detail-right-btn").hide();
						$(".detail-left-btn").hide();
					}else{
						$(".detail-right-btn").show();
						$(".detail-left-btn").show();
					}
					$(".detail-right-btn").click(function(){
						//if(imgCount < imgListLength){
							picCount1 = picCount1 + 1;

							$(".detail-left-btn").show();
							$(".show-deatil-pic img").attr("src","/case_image/"+caseJsonList[showCount].case_image[picCount1].filename);
							for(var i = 0; i < imgListLength; i++){
								$("#surprised li:eq("+i+") img").css("opacity",0);
								$("#surprised li:eq("+i+") img").hide();
							}
							$("#surprised li:eq("+(picCount1)+") img").css("opacity",1);
							$("#surprised li:eq("+picCount1+") img").show();
							if(picCount1+1 == imgListLength){
									console.log(1);
								$(".detail-right-btn").hide();
							}
							
							
						//}
					});
					$(".detail-left-btn").click(function(){
							console.log(picCount1);
							console.log(imgListLength);
							$(".detail-right-btn").show();
							picCount1 = picCount1 - 1;
							$(".show-deatil-pic img").attr("src","case_image/"+caseJsonList[showCount].case_image[picCount1].filename);
							for(var i = 0; i < imgListLength; i++){
								$("#surprised li:eq("+i+") img").css("opacity",0);
								$("#surprised li:eq("+i+") img").hide();
							}
							$("#surprised li:eq("+(picCount1)+") img").css("opacity",1);
							$("#surprised li:eq("+picCount1+") img").show();
							if(picCount1 == 0){
								$(".detail-left-btn").hide();
							}
						//}
					});
					$(".detail-pic").click(function(){
						clearInterval(backTimer);
						$(".show-detail").hide();
						$(".show-deatil-pic").show();
						$(".show-deatil-pic img").attr("src","case_image/"+caseJsonList[showCount].case_image[picCount1].filename);
					});
					var imgListHtml = "";
					console.log(caseJsonList[showCount].case_image);
					for(var i = 0; i < caseJsonList[showCount].case_image.length;i++){
						imgListHtml += "<li><img src = '/case_image/"+caseJsonList[showCount].case_image[i].filename+"'></li>";
					}
					$("#surprised").html(imgListHtml);
					$("#surprised li:eq("+(imgCount)+") img").css("opacity",1);
					changeBackground();
					
				}
			}
		});
	});

	$(".detail-close-btn").click(function(){
		$(".show-detail").show();
		$(".show-deatil-pic").hide();
	});
	$(".show-menu").click(function(){
		// $(".case-show").hide();
		// $(".cases-div").show();
		// $(".case-detail").show();
		// $.ajax({
		// 		url:"http://www.91yahoo.com/caseSearchList.php",
		// 		method:"post",
		// 		success:function(e){
		// 			var caseList = JSON.parse(e);
		// 			var caseListHtml = "";
		// 			for(var i = 0;i<caseList.length;i++){
		// 				caseListHtml+= '<li data-caseId = '+caseList[i].id+'><img src="bg_image/'+caseList[i].bg_image+'"><p class = "case-summary">'+caseList[i].name_en+'</p><p class = "case-summary-chinese">'+caseList[i].name_ch+'</p></li>'
		// 			}
		// 			$("#case-exhibition").html(caseListHtml);
		// 		}
		// 	});
		document.location.href = "http://www.leveldisplay.com/cases.php";
	});
	$(".show-right").click(function(){
		hrefId = parseInt(hrefId) + 1;
		document.location.href = "http://www.leveldisplay.com/cases.php?id="+hrefId;
	});
	$(".show-left").click(function(){
		hrefId = parseInt(hrefId) - 1;
		document.location.href = "http://www.leveldisplay.com/cases.php?id="+hrefId;
	});
	$(".case-detail ul").delegate("li","mouseenter",function(){
		$(this).children(".case-summary").show();
		$(this).children("img").hide();
		$(this).addClass("li-hover");
	});
	$(".case-detail ul").delegate("li","mouseleave",function(){
		$(this).children(".case-summary").hide();
		$(this).children("img").show();
		$(this).removeClass("li-hover");
	});
	function setBackground(){

		$("#surprised").width($(window).width());
		//$("#surprised").height($(window).height());
		$(".footer").height($(window).height()-381);
		if($(window).height() < 520){
			$(".footer").height(123);
		}
		$(".background-img").height($(document).height());
	}
	function backToMenu(){
		var introHeaderHeight = parseInt($(".introduce-header").css("top")) + 60;
		$(".staff-header").animate({top: introHeaderHeight+"px"}, "slow");
		flag = 0;
	}
	function setHeight(){
		$(".background-img").css("height","949px");
		$(".background-img").css("background","url(res/background.png) repeat");

	}
	// var changeUtil = $Class({

		

	// 	_inti:function(){

	// 		this.handler();
	// 		this.timer = "";
	// 		this.autoPlay();
	// 	},
	// 	autoPlay:function(){
	// 		var me = this;
	// 		me.timer = setInterval(function(){
	// 			me.change();
	// 		},8000);
	// 	},
	// 	change:function(){

	// 		var picCount1 = 0;
	// 		var picCount2 = 1;
	// 		var i = 0;
	// 		//标签计数
	// 		var j = 1;
	// 		var maxWidth = $(window).width();
	// 		var listLeng = $("#surprised li").length;
	// 		var flushFlag = false;


	// 		i = i + 1;
	// 		if(i>maxWidth){
	// 			if( picCount1 == listLeng){
	// 				picCount1 = 0;
	// 				setVis($("#surprised li:eq("+picCount1+")").find("img"));
	// 				$(".name a:eq("+picCount1+")").css("display","none");
	// 				$(".name-info p:eq("+picCount1+")").css("display","none");
	// 				picCount1 = picCount1 + 1;
					
	// 			}else{
	// 				setVis($("#surprised li:eq("+picCount1+")").find("img"));
	// 				$(".name a:eq("+picCount1+")").css("display","none");
	// 				$(".name-info p:eq("+picCount1+")").css("display","none");
	// 				picCount1 = picCount1 + 1;
	// 			}
	// 			currentImg = picCount1;
	// 			if(picCount2 == listLeng){
	// 				picCount2 = 0;
	// 				setOpa($("#surprised li:eq("+picCount2+")").find("img"));
	// 				$(".name a:eq("+picCount2+")").css("display","block");
	// 				$(".name-info p:eq("+picCount2+")").css("display","block");
	// 				picCount2 = picCount2 + 1;
	// 			}else{
	// 				setOpa($("#surprised li:eq("+picCount2+")").find("img"));
	// 				$(".name a:eq("+picCount2+")").css("display","block");
	// 				$(".name-info p:eq("+picCount2+")").css("display","block");
	// 				picCount2 = picCount2 + 1;
	// 			}
	// 			i = 0;
	// 		}
	// 		$("#progress-bar").css("left",i+"px");
	// 	},
	// 	handler:function(){
	// 		var me = this;
	// 	}
	// });
	function changeBackground(){
		picCount1 = 0;
		picCount2 = 1;
		var i = 0;
		//标签计数
		var j = 1;
		var maxWidth = $(window).width();
		//alert(maxWidth);
		backTimer = setInterval(changeBackImg,5);
		console.log(backTimer);
		var listLeng = $("#surprised li").length;
		var flushFlag = false;
		console.log("当前"+picCount1);
		function changeBackImg(){
			//alert(i);
			i = i + 1;
			if(i>maxWidth){
				if( picCount1 == listLeng){
					picCount1 = 0;
					setVis($("#surprised li:eq("+picCount1+")").find("img"));
					$(".name a:eq("+picCount1+")").css("display","none");
					$(".name-info p:eq("+picCount1+")").css("display","none");
					picCount1 = picCount1 + 1;
					
				}else{
					setVis($("#surprised li:eq("+picCount1+")").find("img"));
					$(".name a:eq("+picCount1+")").css("display","none");
					$(".name-info p:eq("+picCount1+")").css("display","none");
					picCount1 = picCount1 + 1;
				}
				currentImg = picCount1;
				if(picCount2 == listLeng){
					picCount2 = 0;
					setOpa($("#surprised li:eq("+picCount2+")").find("img"));
					$(".name a:eq("+picCount2+")").css("display","block");
					$(".name-info p:eq("+picCount2+")").css("display","block");
					picCount2 = picCount2 + 1;
				}else{
					setOpa($("#surprised li:eq("+picCount2+")").find("img"));
					$(".name a:eq("+picCount2+")").css("display","block");
					$(".name-info p:eq("+picCount2+")").css("display","block");
					picCount2 = picCount2 + 1;
				}
				i = 0;
			}
			$("#progress-bar").css("left",i+"px");
		}
	}
	//隐藏前一个元素
	function setVis(imgDom){
		var opaImg = 1;
			var timerO = setInterval(runO,10);
			function runO(){
				imgDom.css("z-index","-30");
				opaImg = opaImg - 0.05;
				imgDom.css("opacity",opaImg);
				if(imgDom.css("opacity") == 0){
					clearInterval(timerO);
					imgDom.css("display","none");
				}
			}
	}
	//展示下一个元素
	function setOpa(imgDom){
		var opaImg = 0;
			var timerO = setInterval(runO,10);
			function runO(){
				imgDom.css("z-index","-10");
				imgDom.css("display","block");
				opaImg = opaImg + 0.05;
				imgDom.css("opacity",opaImg);
				if(imgDom.css("opacity") == 1){
					clearInterval(timerO);
				}
			}
	}
});


